package com.tjshop.android.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.Request.Method;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.tjshop.android.GoodsDetailActivity;
import com.tjshop.android.R;
import com.tjshop.android.adapter.FriendsAdapter;
import com.tjshop.android.adapter.GoodsListAdapter;
import com.tjshop.android.constants.UrlConfigs;
import com.tjshop.android.mcv.GlobeVars;
import com.tjshop.android.model.CouponHomeData;
import com.tjshop.android.model.ShopInfo;
import com.tjshop.android.model.cityArea;
import com.tjshop.android.model.tj.json.Category;
import com.tjshop.android.model.tj.json.Goods;
import com.tjshop.android.util.WaitProgressDialog;
import com.tjshop.android.view.AreaPopWindow;
import com.tjshop.android.view.CategoryPopWindow;
import com.tjshop.android.view.InsideListView;
import com.tjshop.android.view.PinnedHeaderListView;
import com.tjshop.android.view.PinnedSectionListView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class FragmentGoodsList extends Fragment implements Handler.Callback {

	private Context mContext;
	private Handler handler;
	private CouponHomeData homeCouponList;
	private ArrayList<ShopInfo> shopListInfos = new ArrayList<ShopInfo>();
	private ArrayList<cityArea> CityAreaInfos = new ArrayList<cityArea>();
	private LayoutInflater inflater;
	private GoodsListAdapter mSimpleAdapter;
	private TextView titleText;
	private CategoryPopWindow categoryPopWindow;
	private AreaPopWindow areaPopWindow;
	private PopupWindow PriceSelectPopupwindow;
	private View popWindowShow;
	private int CurrrentIndex = 0;

	private int priceSelectIndex = 0;
	private int sortSelectIndex = 0;

	private static final int INDEX_CITY_ID = 0;
	private static final int INDEX_LND = 1;
	private static final int INDEX_LAT = 2;
	private static final int INDEX_TOP_KIND = 3;
	private static final int INDEX_SUB_KIND = 4;
	private static final int INDEX_AREA_ID = 5;
	private static final int INDEX_ZONE_ID = 6;
	private static final int INDEX_ATTR_ID = 7;
	private static final int INDEX_MONEY = 8;
	private static final int INDEX_SORT = 9;
	private static final int INDEX_OFFSET = 10;
	private static final int INDEX_BOOK = 11;

	private String[] params = new String[12];
	private WaitProgressDialog mProgressDialog;
	private RequestQueue mQueue;

	public FragmentGoodsList() {
	}

	ArrayList<Goods> goodsList = new ArrayList<Goods>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Arrays.fill(params, "0");
		params[INDEX_CITY_ID] = "13";
		params[INDEX_SORT] = "3";
		if (getArguments() != null && getArguments().getString("gId") != null) {
			params[INDEX_TOP_KIND] = getArguments().getString("gId");
		}

		// getCouponHome(params[INDEX_CITY_ID]);
		// getShopList();
		mProgressDialog = new WaitProgressDialog(getActivity());
		mQueue = Volley.newRequestQueue(getActivity());
		getGoodsList(params[INDEX_TOP_KIND]);
	}

	private void getGoodsList(String string) {
		mProgressDialog.show("获取商品列表信息中", true, false);

		StringRequest stringRequest = new StringRequest(Method.GET,
				UrlConfigs.URL_GOODS_LIST + "?t=" + GlobeVars.token + "&cid="
						+ string + "&p1=&p2=&key=",
				new Response.Listener<String>() {
					@Override
					public void onResponse(String response) {
						JSONArray jsonU;
						try {
							JSONObject obj = new JSONObject(response);
							if (obj.getInt("r") == 0) {
								jsonU = obj.getJSONArray("s");
								// jsonU = new JSONArray(response);
								for (int i = 0; i < jsonU.length(); i++) {
									Goods g = new Goods();
									g.parse(jsonU.getJSONObject(i));
									goodsList.add(g);
								}
								Message message = new Message();
								message.what = 2;
								handler.sendMessage(message);
							} else {
								Message message = new Message();
								message.what = 1;
								handler.sendMessage(message);
							}

						} catch (JSONException e) {
							Message message = new Message();
							message.what = 1;
							handler.sendMessage(message);
							e.printStackTrace();
						}

					}
				}, new Response.ErrorListener() {

					@Override
					public void onErrorResponse(VolleyError error) {
						Message message = new Message();
						message.what = 1;
						handler.sendMessage(message);

					}

				});

		mQueue.add(stringRequest);
	}

	InsideListView listView;
	TextView nearBy_category;

	// 初始化标题栏
	// void initTitleBar(final View mContainer) {
	// nearBy_category = (TextView)
	// mContainer.findViewById(R.id.nearBy_category);
	// mContainer.findViewById(R.id.inc_nearby_dropdown_item).setOnClickListener(new
	// View.OnClickListener() {
	// @Override
	// public void onClick(View view) {
	// if (categoryPopWindow != null&&categoryPopWindow.isShowing()) {
	// categoryPopWindow.dismiss();
	// return;
	// } else {
	// initCategoryPopWindow(mContainer);
	// }
	// }
	// });
	//
	// mContainer.findViewById(R.id.inc_nearby_dropdown_item_location).setOnClickListener(new
	// View.OnClickListener() {
	// @Override
	// public void onClick(View view) {
	// popWindowShow = view;
	// if (CityAreaInfos.size() == 0) {
	// getCityArea(mContainer,params[INDEX_CITY_ID]);
	// }else{
	// initCityAreaPopWindow(mContainer);
	// }
	// }
	// });
	//
	// mContainer.findViewById(R.id.inc_nearby_dropdown_item_filter).setOnClickListener(new
	// View.OnClickListener() {
	// @Override
	// public void onClick(View view) {
	// if (PriceSelectPopupwindow != null&&PriceSelectPopupwindow.isShowing()) {
	// PriceSelectPopupwindow.dismiss();
	// return;
	// } else {
	// initScreeningPopWindow();
	// View parnet =
	// mContainer.findViewById(R.id.inc_nearby_dropdown_item_filter);
	// int[] location = new int[2];
	// parnet.getLocationOnScreen(location);
	// PriceSelectPopupwindow.showAtLocation(parnet, Gravity.NO_GRAVITY,
	// location[0], location[1]+parnet.getHeight());
	// }
	// }
	// });
	//

	// // List<Integer> mPositions = new ArrayList<Integer>();
	// // List<Object> datas = new ArrayList<Object>();
	// // int count = 0;
	// // for(int i = 0;i<shopListInfos.size();i++){
	// // ShopInfo info = shopListInfos.get(i);
	// // datas.add(info);
	// // mPositions.add(count);
	// // count++;
	// // datas.addAll(info.getCoupon_lists());
	// // count+=info.getCoupon_lists().size();
	// // }
	//
	// listView = (PinnedHeaderListView)
	// mContainer.findViewById(R.id.listview_fragment_nearby_shop_list);
	// // mSimpleAdapter = new FriendsAdapter(mContext, datas,
	// mPositions.size(), mPositions);
	// // listView.setAdapter(mSimpleAdapter);
	// // listView.setOnScrollListener(mSimpleAdapter);
	// // listView.setPinnedHeaderView(LayoutInflater.from(mContext).inflate(
	// // R.layout.inc_activity_main_fragment_nearby_list_item_2, listView,
	// false));
	// }

	@Override
	public View onCreateView(LayoutInflater inf, ViewGroup container,
			Bundle savedInstanceState) {
		View v = LayoutInflater.from(getActivity()).inflate(
				R.layout.activity_main_fragment_goodlist, null);
		inflater = inf;
		listView = (InsideListView) v
				.findViewById(R.id.listview_fragment_nearby_shop_list);
		nearBy_category = (TextView) v.findViewById(R.id.nearBy_category);
		nearBy_category.setText(getArguments().getString("name"));
		mContext = this.getActivity();
		handler = new Handler(this);
		// initTitleBar(v);
		return v;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
	}

	@Override
	public void onPause() {
		super.onPause();

		boolean userVisibleHint = getUserVisibleHint();
		Log.w(FragmentGoodsList.class.getSimpleName(),
				"onPause - userVisibleHint: " + userVisibleHint);
	}

	@Override
	public void onResume() {
		super.onResume();

		boolean userVisibleHint = getUserVisibleHint();
		Log.w(FragmentGoodsList.class.getSimpleName(),
				"onResume - userVisibleHint: " + userVisibleHint);
	}

	// public void initCategoryPopWindow(final View mContainer) {
	//
	//
	// final List<keyPair> leftData = new ArrayList<keyPair>();
	// final List<List<keyPair>> rightData = new ArrayList<List<keyPair>>();
	// keyPair k = new keyPair();
	// k.setId("0");
	// k.setName("全部");
	// leftData.add(k);
	// rightData.add(new ArrayList<keyPair>());
	// for(int i = 0;i<homeCouponList.getCategoryList().size();i++){
	// keyPair kp = new keyPair();
	// kp.setId(homeCouponList.getCategoryList().get(i).getId());
	// kp.setName(homeCouponList.getCategoryList().get(i).getTitle());
	// leftData.add(kp);
	// rightData.add(homeCouponList.getCategoryList().get(i).getSublist());
	// }
	// categoryPopWindow = new CategoryPopWindow(mContext, leftData, rightData,
	// new CategoryPopWindow.OnClickListener() {
	//
	// @Override
	// public void onClick(String top_kind, String sub_kind) {
	// TextView tv = (TextView) mContainer.findViewById(R.id.nearBy_category);
	// String tip = "全部";
	// if(sub_kind.equals("0")){
	// for(keyPair kp:leftData){
	// if(kp.getId().equals(top_kind)){
	// tip = kp.getName();
	// }
	// }
	// }else{
	// for(int i = 0;i<leftData.size();i++){
	// keyPair kp = leftData.get(i);
	// if(kp.getId().equals(top_kind)){
	// List<keyPair> data = rightData.get(i);
	// for(keyPair k:data){
	// if(sub_kind .equals(k.getId())){
	// tip = k.getName();
	// }
	// }
	// }
	// }
	// }
	//
	// tv.setText(tip);
	// params[INDEX_TOP_KIND] = top_kind;
	// params[INDEX_SUB_KIND] = sub_kind;
	// getShopList();
	// }
	// });
	// categoryPopWindow.showPopupWindow(mContainer.findViewById(R.id.inc_nearby_dropdown_item));
	// // initCategoryPopWindow();
	// // popWindowShow = view;
	// // categoryPopWindow.showAsDropDown(view, 0, 5);
	// }

	// private void getShopList() {
	// getShopList(params[INDEX_CITY_ID],params[INDEX_LND]
	// ,params[INDEX_LAT],params[INDEX_TOP_KIND],
	// params[INDEX_SUB_KIND],params[INDEX_AREA_ID],
	// params[INDEX_ZONE_ID],params[INDEX_ATTR_ID]
	// ,params[INDEX_MONEY],params[INDEX_SORT]
	// ,offset+"",params[INDEX_BOOK]);
	// }

	// public void initScreeningPopWindow() {
	// // // 获取自定义布局文件pop.xml的视图
	// View customView = inflater.inflate(R.layout.priceselectpopwindow,
	// null, false);
	// // 创建PopupWindow实例,200,150分别是宽度和高度
	// PriceSelectPopupwindow = new PopupWindow(customView,
	// ViewGroup.LayoutParams.FILL_PARENT,
	// ViewGroup.LayoutParams.WRAP_CONTENT);
	// // 设置动画效果 [R.style.AnimationFade 是自己事先定义好的]
	// // PriceSelectPopupwindow.setAnimationStyle(R.style.AnimationFade);
	// PriceSelectPopupwindow.setFocusable(true);
	// PriceSelectPopupwindow.setTouchable(true);
	// PriceSelectPopupwindow.setOutsideTouchable(true);
	//
	// /** 在这里可以实现自定义视图的功能 */
	// final CheckBox NeedBook = (CheckBox)
	// customView.findViewById(R.id.needBook);
	// final TextView PriceArea0 = (TextView)
	// customView.findViewById(R.id.PriceArea0);
	// final TextView PriceArea1 = (TextView)
	// customView.findViewById(R.id.PriceArea1);
	// final TextView PriceArea2 = (TextView)
	// customView.findViewById(R.id.PriceArea2);
	// final TextView PriceArea3 = (TextView)
	// customView.findViewById(R.id.PriceArea3);
	// final TextView PriceArea4 = (TextView)
	// customView.findViewById(R.id.PriceArea4);
	// final TextView sortCategory1 = (TextView)
	// customView.findViewById(R.id.sortCategory1);
	// final TextView sortCategory2 = (TextView)
	// customView.findViewById(R.id.sortCategory2);
	// final TextView sortCategory3 = (TextView)
	// customView.findViewById(R.id.sortCategory3);
	// final TextView sortCategory4 = (TextView)
	// customView.findViewById(R.id.sortCategory4);
	// final TextView sortCategory5 = (TextView)
	// customView.findViewById(R.id.sortCategory5);
	// final Button okBtn = (Button) customView.findViewById(R.id.okBtn);
	// PriceArea0.setOnTouchListener(new View.OnTouchListener() {
	// @Override
	// public boolean onTouch(View v, MotionEvent event) {
	// priceSelectIndex = 0;
	// PriceArea0.setBackgroundColor(0x66808080);
	// PriceArea1.setBackgroundColor(Color.rgb(255,255,255));
	// PriceArea2.setBackgroundColor(Color.rgb(255,255,255));
	// PriceArea3.setBackgroundColor(Color.rgb(255,255,255));
	// PriceArea4.setBackgroundColor(Color.rgb(255,255,255));
	// return false;
	// }
	// });
	// PriceArea1.setOnTouchListener(new View.OnTouchListener() {
	// @Override
	// public boolean onTouch(View v, MotionEvent event) {
	// priceSelectIndex = 1;
	// PriceArea0.setBackgroundColor(0x66808080);
	// PriceArea0.setBackgroundColor(Color.rgb(255,255,255));
	// PriceArea2.setBackgroundColor(Color.rgb(255,255,255));
	// PriceArea3.setBackgroundColor(Color.rgb(255,255,255));
	// PriceArea4.setBackgroundColor(Color.rgb(255,255,255));
	// return false;
	// }
	// });
	// PriceArea2.setOnTouchListener(new View.OnTouchListener() {
	// @Override
	// public boolean onTouch(View v, MotionEvent event) {
	// priceSelectIndex = 2;
	// PriceArea2.setBackgroundColor(0x66808080);
	// PriceArea1.setBackgroundColor(Color.rgb(255,255,255));
	// PriceArea0.setBackgroundColor(Color.rgb(255,255,255));
	// PriceArea3.setBackgroundColor(Color.rgb(255,255,255));
	// PriceArea4.setBackgroundColor(Color.rgb(255,255,255));
	// return false;
	// }
	// });
	// PriceArea3.setOnTouchListener(new View.OnTouchListener() {
	// @Override
	// public boolean onTouch(View v, MotionEvent event) {
	// priceSelectIndex = 3;
	// PriceArea3.setBackgroundColor(0x66808080);
	// PriceArea1.setBackgroundColor(Color.rgb(255,255,255));
	// PriceArea2.setBackgroundColor(Color.rgb(255,255,255));
	// PriceArea0.setBackgroundColor(Color.rgb(255,255,255));
	// PriceArea4.setBackgroundColor(Color.rgb(255,255,255));
	// return false;
	// }
	// });
	// PriceArea4.setOnTouchListener(new View.OnTouchListener() {
	// @Override
	// public boolean onTouch(View v, MotionEvent event) {
	// priceSelectIndex = 4;
	// PriceArea4.setBackgroundColor(0x66808080);
	// PriceArea1.setBackgroundColor(Color.rgb(255,255,255));
	// PriceArea2.setBackgroundColor(Color.rgb(255,255,255));
	// PriceArea3.setBackgroundColor(Color.rgb(255,255,255));
	// PriceArea0.setBackgroundColor(Color.rgb(255,255,255));
	// return false;
	// }
	// });
	// sortCategory1.setOnTouchListener(new View.OnTouchListener() {
	// @Override
	// public boolean onTouch(View v, MotionEvent event) {
	// sortSelectIndex = 0;
	// sortCategory1.setBackgroundColor(0x66808080);
	// sortCategory2.setBackgroundColor(Color.rgb(255,255,255));
	// sortCategory3.setBackgroundColor(Color.rgb(255,255,255));
	// sortCategory4.setBackgroundColor(Color.rgb(255,255,255));
	// sortCategory5.setBackgroundColor(Color.rgb(255,255,255));
	// return false;
	// }
	// });
	// sortCategory2.setOnTouchListener(new View.OnTouchListener() {
	// @Override
	// public boolean onTouch(View v, MotionEvent event) {
	// sortSelectIndex = 1;
	// sortCategory2.setBackgroundColor(0x66808080);
	// sortCategory1.setBackgroundColor(Color.rgb(255,255,255));
	// sortCategory3.setBackgroundColor(Color.rgb(255,255,255));
	// sortCategory4.setBackgroundColor(Color.rgb(255,255,255));
	// sortCategory5.setBackgroundColor(Color.rgb(255,255,255));
	// return false;
	// }
	// });
	// sortCategory3.setOnTouchListener(new View.OnTouchListener() {
	// @Override
	// public boolean onTouch(View v, MotionEvent event) {
	// sortSelectIndex = 2;
	// sortCategory3.setBackgroundColor(0x66808080);
	// sortCategory2.setBackgroundColor(Color.rgb(255,255,255));
	// sortCategory1.setBackgroundColor(Color.rgb(255,255,255));
	// sortCategory4.setBackgroundColor(Color.rgb(255,255,255));
	// sortCategory5.setBackgroundColor(Color.rgb(255,255,255));
	// return false;
	// }
	// });
	// sortCategory4.setOnTouchListener(new View.OnTouchListener() {
	// @Override
	// public boolean onTouch(View v, MotionEvent event) {
	// sortSelectIndex = 3;
	// sortCategory4.setBackgroundColor(0x66808080);
	// sortCategory2.setBackgroundColor(Color.rgb(255,255,255));
	// sortCategory3.setBackgroundColor(Color.rgb(255,255,255));
	// sortCategory1.setBackgroundColor(Color.rgb(255,255,255));
	// sortCategory5.setBackgroundColor(Color.rgb(255,255,255));
	// return false;
	// }
	// });
	// sortCategory5.setOnTouchListener(new View.OnTouchListener() {
	// @Override
	// public boolean onTouch(View v, MotionEvent event) {
	// sortSelectIndex = 4;
	// sortCategory5.setBackgroundColor(0x66808080);
	// sortCategory2.setBackgroundColor(Color.rgb(255,255,255));
	// sortCategory3.setBackgroundColor(Color.rgb(255,255,255));
	// sortCategory4.setBackgroundColor(Color.rgb(255,255,255));
	// sortCategory1.setBackgroundColor(Color.rgb(255,255,255));
	// return false;
	// }
	// });
	// okBtn.setOnTouchListener(new View.OnTouchListener() {
	// @Override
	// public boolean onTouch(View v, MotionEvent event) {
	// params[INDEX_MONEY] = priceSelectIndex+"";
	// params[INDEX_SORT] = sortSelectIndex+"";
	// params[INDEX_BOOK] = NeedBook.isChecked()?1+"":0+"";
	// getShopList();
	// if (PriceSelectPopupwindow != null && PriceSelectPopupwindow.isShowing())
	// {
	// PriceSelectPopupwindow.dismiss();
	// PriceSelectPopupwindow = null;
	// }
	// return false;
	// }
	// });
	//
	// PriceSelectPopupwindow.setOutsideTouchable(true);
	// }

	// public void initCityAreaPopWindow(final View mContainer) {
	//
	// final List<keyPair> leftData = new ArrayList<keyPair>();
	// final List<List<keyPair>> rightData = new ArrayList<List<keyPair>>();
	// keyPair k = new keyPair();
	// k.setId("0");
	// k.setName("全部");
	// leftData.add(k);
	// rightData.add(new ArrayList<keyPair>());
	// for(int i = 0;i<CityAreaInfos.size();i++){
	// keyPair kp = new keyPair();
	// kp.setId(CityAreaInfos.get(i).getArea_id());
	// kp.setName(CityAreaInfos.get(i).getArea_name());
	// leftData.add(kp);
	// rightData.add(CityAreaInfos.get(i).getZone_list());
	// }
	// areaPopWindow = new AreaPopWindow(mContext, leftData, rightData, new
	// AreaPopWindow.OnClickListener() {
	//
	// @Override
	// public void onClick(String area_id, String zone_id) {
	// TextView tv = (TextView) mContainer.findViewById(R.id.area_text);
	// String tip = "全部";
	// if(zone_id.equals("0")){
	// for(keyPair kp:leftData){
	// if(kp.getId().equals(area_id)){
	// tip = kp.getName();
	// }
	// }
	// }else{
	// for(int i = 0;i<leftData.size();i++){
	// keyPair kp = leftData.get(i);
	// if(kp.getId().equals(area_id)){
	// List<keyPair> data = rightData.get(i);
	// for(keyPair k:data){
	// if(zone_id .equals(k.getId())){
	// tip = k.getName();
	// }
	// }
	// }
	// }
	// }
	//
	// tv.setText(tip);
	// params[INDEX_AREA_ID] = area_id;
	// params[INDEX_ZONE_ID] = zone_id;
	// getShopList();
	// }
	// });
	// areaPopWindow.showPopupWindow(mContainer.findViewById(R.id.inc_nearby_dropdown_item));
	// }
	//
	// void getCouponHome(String city_id ){
	// String md5 = MD5Utils.md5(city_id + AppConfig.SignKey);
	// final String param = "city_id="+city_id+"&sign="+md5;
	// new Thread(new Runnable() {
	// @Override
	// public void run() {
	// String ret = HttpUtil.sendPost(AppConfig.HomeUrl, param);
	// if(!ret.equals("")){
	// homeCouponList = JsonParser.getHomeData(ret);
	// handler.sendEmptyMessage(100);
	// }
	// }
	// }).start();
	// }

	// void getCityArea(final View mContainer, String city_id ){
	// String md5 = MD5Utils.md5(city_id + AppConfig.SignKey);
	// final String param = "city_id="+city_id+"&sign="+md5;
	// new Thread(new Runnable() {
	// @Override
	// public void run() {
	// Message message = new Message();
	// String ret = HttpUtil.sendPost(AppConfig.CityAreaUrl, param);
	// if(!ret.equals("")){
	// try {
	// JSONObject json = new JSONObject(ret);
	// if (json.getInt("code") == 0){
	// JSONArray data = json.getJSONArray("data");
	// for(int i = 0;i<data.length();i++) {
	// JSONObject item = data.getJSONObject(i);
	// cityArea info = new cityArea();
	// info.setArea_id(item.getString("area_id"));
	// info.setArea_name(item.getString("area_name"));
	// JSONArray zoneList = item.getJSONArray("zone_list");
	// ArrayList<keyPair> zone_list = new ArrayList<keyPair>();
	// for (int j=0;j<zoneList.length();j++){
	// keyPair keyValues = new keyPair();
	// JSONObject item_zone_list = zoneList.getJSONObject(j);
	// keyValues.setId(item_zone_list.getString("zone_id"));
	// keyValues.setName(item_zone_list.getString("zone_name"));
	// zone_list.add(keyValues);
	// }
	// info.setZone_list(zone_list);
	// CityAreaInfos.add(info);
	// }
	// message.what = 4;
	// message.obj = mContainer;
	// handler.sendMessage(message);
	// }else{
	// message.what = 5;
	// handler.sendMessage(message);
	// }
	// }catch(JSONException e){
	// message.what = 5;
	// handler.sendMessage(message);
	// e.printStackTrace();
	// }
	// }
	//
	// }
	// }).start();
	// }
	// private int offset;
	// void getShopList(String city_id,String Lng,String Lat ,String
	// Top_kind,String Sub_kind,String Area_id,String Zone_id,String
	// Attr_id,String Money,String Sort,String Offset,String Book ){
	// String md5 = MD5Utils.md5(city_id + Lng + Lat+ Top_kind+ Sub_kind+
	// Area_id+ Zone_id+ Attr_id+ Money+ Sort+ Offset+ Book+AppConfig.SignKey);
	// final String param =
	// "lng="+Lng+"&lat="+Lat+"&offset="+Offset+"&sort="+Sort+"&area_id="+Area_id+"&zone_id="+Zone_id+"&top_kind="
	// +Top_kind+"&sub_kid="+Sub_kind+"&attr_id="+Attr_id+"&money="+Money+"&city_id="+city_id+"&book="+Book+"&sign="+md5;
	// new Thread(new Runnable() {
	// @Override
	// public void run() {
	// Message message = new Message();
	// String ret = HttpUtil.sendPost(AppConfig.CouponUrl, param);
	// if(!ret.equals("")){
	// try {
	// shopListInfos = new ArrayList<ShopInfo>();
	// JSONObject json = new JSONObject(ret);
	// if (json.getInt("code") == 0){
	// JSONObject data = json.getJSONObject("data");
	// // offset = data.getInt("offset");
	// JSONArray list = data.getJSONArray("list");
	// for(int i = 0;i<list.length();i++) {
	// JSONObject subListItem = list.getJSONObject(i);
	// ShopInfo info = new ShopInfo();
	// info.parse(subListItem);
	// shopListInfos.add(info);
	// }
	// message.what = 2;
	// handler.sendMessage(message);
	// }else{
	// message.what = 3;
	// handler.sendMessage(message);
	// }
	// }catch(JSONException e){
	// message.what = 3;
	// handler.sendMessage(message);
	// e.printStackTrace();
	// }
	// }
	// }
	// }).start();
	// }

	@Override
	public boolean handleMessage(Message message) {
		switch (message.what) {
		case 0:
			mProgressDialog.dismiss();
			Toast.makeText(mContext, "获取数据成功！", Toast.LENGTH_SHORT).show();
			break;
		case 1:
			mProgressDialog.dismiss();
			Toast.makeText(mContext, "获取数据失败!", Toast.LENGTH_SHORT).show();
			break;
		case 2:
			mProgressDialog.dismiss();
//			Toast.makeText(mContext, "获取优惠券成功！", Toast.LENGTH_SHORT).show();

			mSimpleAdapter = new GoodsListAdapter(mContext, goodsList, inflater);
			listView.setAdapter(mSimpleAdapter);
			// 设置item点击的监听器
			listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					 Intent intent = new Intent(inflater.getContext(),
					 GoodsDetailActivity.class);
					 intent.putExtra("gId",goodsList.get(arg2).getGoods_id());
					 inflater.getContext().startActivity(intent);
//					Toast.makeText(mContext, "正在开发中", Toast.LENGTH_SHORT)
//							.show();
				}
			});

			// mSimpleAdapter.notifyDataSetChanged();
			// List<Integer> mPositions = new ArrayList<Integer>();
			// List<ShopInfo> datas = new ArrayList<ShopInfo>();
			// final List<CouponDetail> itemData = new
			// ArrayList<CouponDetail>();
			// int count = 0;
			// for(int i = 0;i<shopListInfos.size();i++){
			// ShopInfo info = shopListInfos.get(i);
			// datas.add(info);
			// mPositions.add(count);
			// itemData.addAll(info.getCoupon_lists());
			// count+=info.getCoupon_lists().size();
			// }
			//
			// if(itemData.size()==0 || datas.size()==0){
			// listView.setAdapter(null);
			// listView.setOnScrollListener(null);
			// listView.setPinnedHeaderView(null);
			//
			// break;
			// }
			// mSimpleAdapter = new FriendsAdapter(mContext, datas,itemData,
			// mPositions.size(), mPositions);
			// listView.setAdapter(mSimpleAdapter);
			// listView.setOnScrollListener(mSimpleAdapter);
			// listView.setPinnedHeaderView(LayoutInflater.from(mContext).inflate(
			// R.layout.inc_activity_main_fragment_nearby_list_item_2, listView,
			// false));
			// listView.setOnItemClickListener(new OnItemClickListener() {
			//
			// @Override
			// public void onItemClick(AdapterView<?> arg0, View arg1,
			// int arg2, long arg3) {
			// Intent intent = new Intent(inflater.getContext(),
			// CouponDetailActivity.class);
			// intent.putExtra("coupon_id",itemData.get(arg2).getCoupon_id());
			// inflater.getContext().startActivity(intent);
			// }
			// });
			break;
		case 3:
			Toast.makeText(mContext, "获取优惠券失败!", Toast.LENGTH_SHORT).show();
			mSimpleAdapter.notifyDataSetChanged();
			break;
		case 4:
			Toast.makeText(mContext, "获取城市区域信息成功！", Toast.LENGTH_SHORT).show();

			// initCityAreaPopWindow((View) message.obj);
			break;
		case 5:
			Toast.makeText(mContext, "获取城市区域信息失败!", Toast.LENGTH_SHORT).show();
			break;
		case 100:
			// if(homeCouponList.getCategoryList()!=null &&
			// homeCouponList.getCategoryList().size()!=0){
			// if(params[INDEX_TOP_KIND] != "0"){
			// String name = "全部";
			// for(Category c:homeCouponList.getCategoryList()){
			// if(c.getId().equals(params[INDEX_TOP_KIND])){
			// name = c.getTitle();
			// }
			// }
			// nearBy_category.setText(name);
			// }
			// }
			break;
		default:
			break;
		}
		return false;
	}

}
